import ajax from '../../utils/request'
import { concatUrl, encodeURI } from '@/utils'
import './types'
/**
 * 添加车辆
 * @param data
 */
export function fetchAdd(data: CarInfo, fields: string | object = 'fields=currentMileage,carLicense,carModelLabel,id,isDefault,isNewEnergy'): any {
  return ajax({
    method: 'POST',
    url: concatUrl('/v1/cars', fields),
    data
  })
}

/**
 * 获取车辆信息
 */
export function fetchList(data: CarList, fields: string | object = 'fields=currentMileage,modelPicUrl,carLicense,carModelLabel,id,isDefault,isCertified,isNewEnergy,licenseProvince,licenseLetter,licenseNumber,jekunModelCode,seriesId,seriesName,modelId,modelName,brandId,brandName,brandLogoUrl,carImageUrl'): any {
  // console.log('data', encodeURIComponent(JSON.stringify(data)))
  return ajax({
    method: 'GET',
    url: concatUrl('/v1/cars', fields),
    data: encodeURI(data)
  })
}

/**
 * 更新车辆信息
 * @param data
 */
export function fetchUpdate(data: CarInfo): any {
  let id = data.id
  delete data.id
  return ajax({
    method: 'PUT',
    url: `/v1/cars/${id}?fields=carLicense,modelId`,
    data
  })
}

/**
 * 设置为默认车辆
 * @param carId
 */
export function fetchSetDefault(carId: number): any {
  return ajax({
    method: 'PUT',
    url: `/v1/cars/${carId}/default?fields=carLicense`,
    // data: {}
  })
}

/**
 * 取消设置默认车辆
 * @param carId
 */

export function fetchCancelDefault(carId: number): any {
  return ajax({
    method: 'PUT',
    url: `/v1/cars/${carId}/cancel-default`,
    // data: {}
  })
}

/**
 *  删除车辆信息
 * @param carId
 */
export function fetchDel(carId: number): any {
  return ajax({
    method: 'DELETE',
    url: `/v1/cars/${carId}`
  })
}

/**
 *  获取车辆品牌
 */
export function fetchCarBrands() {
  return ajax({
    method: 'GET',
    url: `${PUB_API_BASH_URL}/v1/car/car-brands`,
  })
}

/**
 *  获取品牌下的车系
 */
export function fetchCarSeries(brandId): any {
  return ajax({
    method: 'GET',
    url: `${PUB_API_BASH_URL}/v1/car/car-producer-series`,
    data: {
      brandId
    }
  })
}

/**
 *  获取车辆品牌
 */
export function fetchCarModel(seriesId: number): any {
  return ajax({
    method: 'GET',
    url: `${PUB_API_BASH_URL}/v1/car/car-model`,
    data: {
      seriesId
    }
  })
}

// 查询用户最多汽车数量
export function queryUserCarInfo() { //{max:3} -1 为不限制
  return ajax({
    url: '/v1/car/info',
    method: 'GET'
  })
}

/**
 * 添加车辆
 * @param data
 */
export function updateMileage(data): any {
  return ajax({
    method: 'POST',
    url: concatUrl('/v1/car/update-mileage'),
    data
  })
}
